interface StringValidator {
    isAcceptable(s: string): boolean;
}

let regLetters = /^[A-Za-z]+$/;
let regNumbers = /^[0-9]+$/;

class LettersOnlyValidator implements StringValidator {
    isAcceptable(s: string) {
        return regLetters.test(s);
    }
}

class ZipCodeValidator implements StringValidator {
    isAcceptable(s: string) {
        return s.length === 5 && regNumbers.test(s);
    }
}

// Validators to use
// validators是一个以任意string做键 StringValidator为值的对象
let validators: { [s: string]: StringValidator } = {};

/* 向validators中添加key-value */
validators["ZIP code"] = new ZipCodeValidator();
validators["Letters only"] = new LettersOnlyValidator();

// Some samples to try
let strings = ["Hello", "98052", "101"];
for (let s of strings) {
    for (let name in validators) {
        let isMatch = validators[name].isAcceptable(s);
        console.log(
            `'${s}' ${isMatch ? "matches" : "does not match"} '${name}'.`
        );
    }
}
